/*
 * <p>Copyright©www.breo.com 2020-2022</p>
 * <p>FileName: EventConsumerListener</p>
 * <p>Author:   ke.qi</p>
 * <p>Date:     2022/11/17 19:00</p>
 * <p>Description: </p>
 */

package com.greate.community.event;

import com.alibaba.fastjson.JSONObject;
import com.greate.community.entity.Event;
import com.greate.community.entity.Message;
import com.greate.community.service.DiscussPostService;
import com.greate.community.service.MessageService;
import com.greate.community.util.CommunityConstant;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * @author ke.qi
 */
@Component
@Slf4j
public class EventConsumerListener implements ApplicationListener<SendDisEvent> {


    @Autowired
    private DiscussPostService discussPostService;

    // @Autowired
    // private ElasticsearchService elasticsearchService;

    @Autowired
    private MessageService messageService;

    @Override
    public void onApplicationEvent(SendDisEvent sendDisEvent) {
        send(sendDisEvent.getEvent());
    }

    private void send(Event event) {

        log.info("消费数据: {}", event);

        String topic = event.getTopic();

        switch (topic) {

            case CommunityConstant.TOPIC_PUBLISH:

                handlePublishMessage(event);

                break;

            case CommunityConstant.TOPIC_COMMNET:
            case CommunityConstant.TOPIC_LIKE:
            case CommunityConstant.TOPIC_FOLLOW:

                handleMessage(event);

                return;

            case CommunityConstant.TOPIC_DELETE:

                handleDeleteMessage(event);

                return;
            default:
                throw new IllegalStateException("Unexpected value: " + topic);
        }
    }


    public void handlePublishMessage(Event event) {

        // Event event = JSONObject.parseObject(record.value().toString(), Event.class);
        // if (event == null) {
        //     logger.error("消息格式错误");
        //     return ;
        // }

        // DiscussPost post = discussPostService.findDiscussPostById(event.getEntityId());
        // elasticsearchService.saveDiscusspost(post);

    }

    public void handleMessage(Event event) {
        // if (record == null || record.value() == null) {
        //     logger.error("消息的内容为空");
        //     return ;
        // }
        // Event event = JSONObject.parseObject(record.value().toString(), Event.class);
        // if (event == null) {
        //     logger.error("消息格式错误");
        //     return ;
        // }

        // 发送系统通知
        Message message = new Message();
        message.setFromId(CommunityConstant.SYSTEM_USER_ID);
        message.setToId(event.getEntityUserId());
        message.setConversationId(event.getTopic());
        message.setCreateTime(new Date());

        Map<String, Object> content = new HashMap<>();
        content.put("userId", event.getUserId());
        content.put("entityType", event.getEntityType());
        content.put("entityId", event.getEntityId());
        if (!event.getData().isEmpty()) { // 存储 Event 中的 Data
            for (Map.Entry<String, Object> entry : event.getData().entrySet()) {
                content.put(entry.getKey(), entry.getValue());
            }
        }
        message.setContent(JSONObject.toJSONString(content));

        messageService.addMessage(message);

    }

    public void handleDeleteMessage(Event event) {

        // elasticsearchService.deleteDiscusspost(event.getEntityId());

    }

}